package org.jjj.market.service.impl;

import org.jjj.market.domain.Business;
import org.jjj.market.domain.BusinessProduct;
import org.jjj.market.domain.Clue;
import org.jjj.market.domain.ClueRemark;
import org.jjj.market.dto.ClueActivityDto;
import org.jjj.market.dto.ClueBusinessDto;
import org.jjj.market.mapper.*;
import org.jjj.market.service.IClueService;
import org.jjj.market.vo.ShowPieChartVO;
import org.jjj.org.basic.service.impl.BaseServiceImpl;
import org.jjj.org.domain.Employee;
import org.jjj.prod.domain.Product;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.jjj.org.basic.utils.LoginContext;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangjianjun
 * @since 2022-10-24
 */
@Service
public class ClueServiceImpl extends BaseServiceImpl<Clue> implements IClueService {
@Autowired
private ClueActivityMapper clueActivityMapper;
@Autowired
private ClueMapper clueMapper;
@Autowired
private ClueRemarkMapper clueRemarkMapper;
@Autowired
private BusinessMapper businessMapper;
@Autowired
private BusinessProductMapper businessProductMapper;

    /**
     * 向关联表中新增线索绑定的活动
     * @param dto
     */
    @Override
    @Transactional
    public void clueBindActivities(ClueActivityDto dto) {
        Long clueId = dto.getClueId();
        List<Long> activityIds = dto.getActivityIds();
        //新增之前先清空一下对应clueid的数据，避免重复
        clueActivityMapper.deleteByClueId(clueId);
        //批量新增
        clueActivityMapper.batchActivities(clueId, activityIds);
    }

    /**
     * 当前端分配完线索之后需要提交到后端的线索库中
     * @param clue
     */
    @Override
    @Transactional
    public void assignClue(Clue clue) {
        //修改线索信息
        clue.setState(1);
        clue.setEditTime(new Date());
        clue.setEditBy(LoginContext.getLoginUser().getId()+"");
        // 调用修改方法 修改线索数据
        clueMapper.update(clue);
        //每 一次线索的更改，分配,跟进,变更等操作都要记录到线索备注里面
        ClueRemark remark = new ClueRemark();
        remark.setClueId(clue.getId());
        remark.setNoteContent(clue.getFullName()+"被分配给"+clue.getSeller().getUsername()+",操作人:"+LoginContext.getLoginUser().getUsername());
        remark.setCreateTime(new Date());
        remark.setCreateBy(LoginContext.getLoginUser().getId());
        clueRemarkMapper.insert(remark);
    }

    /**
     * 线索转化商机，提交的数据保存到数据库
     * @param dto
     */
    @Override
    @Transactional
    public void clueChangeBusiness(ClueBusinessDto dto) {
        Clue clue = dto.getClue();
        Employee loginUser = LoginContext.getLoginUser();
        Product product = dto.getProduct();

        // 1.修改线索的状态 = 2已转化商机 修改人  修改时间
        clue.setState(2);
        clue.setEditTime(new Date());
        clue.setEditBy(loginUser.getId()+"");
        //更新线索表
        clueMapper.update(clue);
        // 2.记录线索操作备注到线索备注表
        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setNoteContent(clue.getFullName()+"已被转化为商机，"+"操作人为："+loginUser.getUsername());
        clueRemark.setClueId(clue.getId());
        clueRemark.setCreateBy(loginUser.getId());
        clueRemark.setCreateTime(new Date());
        //备注表更新
        clueRemarkMapper.insert(clueRemark);
        // 3.创建商机 - 新增一条数据到t_mkt_business
        Business business = new Business();
        business.setName("客户"+clue.getFullName()+"对"+product.getName()+"产品有购买意向");
        business.setClueId(clue.getId());
        business.setProductId(product.getId());
        business.setProductName(product.getName());
        business.setProductPrice(product.getPrice());
        business.setState(0);
        //新增一条商机到数据库
        businessMapper.insert(business);
        // 4.将产生商机这一刻的产品信息存放到 t_mkt_business_product
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(product, businessProduct);
        businessProduct.setId(null);
        businessProduct.setBusinessId(business.getId());
        businessProduct.setProductId(product.getId());
        //新增产品信息到商机产品表中
        businessProductMapper.insert(businessProduct);
    }

    /**
     * 查询出所有的线索来源
     * @return
     */
    @Override
    public List<ShowPieChartVO> getPieChartData() {
        return clueMapper.getPieChartData();
    }
}
